<?php

namespace common\models\datas;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use common\components\extenders\MenuManage;

/**
 * This is the model class for table "{{%integral_task}}".
 *
 * @property integer $task_id
 * @property string $task_name
 * @property string $task_type
 * @property string $task_standard
 * @property string $honor
 * @property string $for_customer
 * @property string $complete_location
 * @property string $task_attr
 * @property integer $task_count
 * @property integer $is_dispaly
 * @property integer $intergral_val
 * @property integer $user_id
 * @property integer $created_at
 */
class IntegralTask extends \common\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%integral_task}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['task_standard'], 'string'],
            [['task_count', 'is_dispaly', 'intergral_val', 'user_id', 'created_at', 'updated_at'], 'integer'],
            [['task_name', 'task_type', 'task_standard', 'honor', 'task_count', 'for_customer', 'complete_location', 'task_attr', 'is_dispaly', 'intergral_val'], 'required'],
            [['task_name', 'task_type', 'honor', 'for_customer', 'complete_location', 'task_attr'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'task_id' => Yii::t('common', '任务编号'),
            'task_name' => Yii::t('common', '任务名称'),
            'task_type' => Yii::t('common', '任务分类'),
            'task_standard' => Yii::t('common', '任务标准'),
            'honor' => Yii::t('common', '荣誉名称'),
            'for_customer' => Yii::t('common', '适用人群'),
            'complete_location' => Yii::t('common', '完成地点'),
            'task_attr' => Yii::t('common', '任务属性'),
            'task_count' => Yii::t('common', '任务总数量'),
            'is_dispaly' => Yii::t('common', '是否隐藏任务'),
            'intergral_val' => Yii::t('common', '达成获得分值'),
            'user_id' => Yii::t('common', '添加用户'),
            'created_at' => Yii::t('common', '添加时间'),
            'updated_at' => Yii::t('common', '修改时间'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['is_dispaly'] = function () {
            switch ($this->is_dispaly) {
                case 0:
                    return \Yii::t('common', '否');
                case 1:
                    return \Yii::t('common', '是');
            }
        };
        $fields['for_customer'] = function () {
            switch ($this->for_customer) {
                case 0:
                    return \Yii::t('common', '会员');
                case 1:
                    return \Yii::t('common', '非会员');
            }
        };
        $fields['complete_location'] = function () {
            switch ($this->complete_location) {
                case 0:
                    return \Yii::t('common', '广场内部');
                case 1:
                    return \Yii::t('common', '会员家中');
            }
        };
        $fields['task_attr'] = function () {
            switch ($this->task_attr) {
                case 0:
                    return \Yii::t('common', '奖励任务');
                case 1:
                    return \Yii::t('common', '惩罚任务');
            }
        };
        $fields['honor'] = function () {
            switch ($this->honor) {
                case 0:
                    return \Yii::t('common', '红包达人');
                case 1:
                    return \Yii::t('common', '小大人');
                case 2:
                    return \Yii::t('common', '点评达人');
                case 3:
                    return \Yii::t('common', '勤劳达人');
                case 4:
                    return \Yii::t('common', '分享达人');
                case 5:
                    return \Yii::t('common', '礼貌达人');
                case 6:
                    return \Yii::t('common', '卫生达人');
                case 7:
                    return \Yii::t('common', '好学达人');
                case 8:
                    return \Yii::t('common', '知识达人');
                case 9:
                    return \Yii::t('common', '环保');
            }
        };
        $fields['task_type'] = function () {
            switch ($this->task_type) {
                case 0:
                    return \Yii::t('common', '参与互动');
                case 1:
                    return \Yii::t('common', '习惯养成');
                case 2:
                    return \Yii::t('common', '教育教学');
            }
        };
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['updated_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->updated_at);
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'admin' => function () {
                return $this->admin->username;
            },
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ]);
    }

    public function getAdmin()
    {
        return $this->hasOne(Admin::className(), ['id' => 'user_id']);
    }
}
